<?php

namespace App\Http\Controllers\Admin\v1;

use App\Http\Controllers\Controller;
use App\Models\ApiDocument;
use App\Models\ApiGateway;
use App\Services\Utils\Utils;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;
use App\Models\Customer;
use App\Models\ApiKey;
use App\Models\ApiKeyRequestLog;
use App\Models\TimePeriod;
use App\Services\Utils\Guard;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\JWTGuard;
use App\Exceptions\ApiAbstractException;
use App\Exceptions\ApiErrCode;
use App\Http\Requests\Admin\v1\CreateApiKeyRequest;
use Illuminate\Support\Carbon;

class ApiKeyController extends ApiController
{
    public function info(Request $request)
    {
        $apikey_id = $request->get('apikey_id');
        $apikey = ApiKey::find($apikey_id);
        $this->checkParamError($apikey, trans('checker.check_apikey_id_error'));
        return $this->response(compact('apikey'));
    }

    public function create(CreateApiKeyRequest $request)
    {
        $api_key = new ApiKey;
        $api_key->customer_id = $request->get('customer_id');
        $api_key->key = $api_key->key;
        $api_key->secret = $api_key->secret;
        $api_key->name = $request->name;
        $api_key->description = $request->description;
        $api_key->type = $request->type;
        $api_key->save();

        $apikey = $api_key->findByKey($api_key->key);
        return $this->response(compact('apikey'));
    }

    public function update(Request $request)
    {
        $apikey_id = $request->apikey_id;

        $apikey = ApiKey::find($apikey_id);
        $this->checkParamError($apikey, trans('checker.check_apikey_id_error'));

        $apikey->status = $request->status;
        $apikey->name = $request->name;
        $apikey->description = $request->description;
        $apikey->save();
        return $this->response(compact('apikey'));
    }

    public function apikeys(Request $request)
    {
        $per_page = $request->per_page;
        $user = Guard::customer();
        $apikeys = ApiKey::where('customer_id', $user['id'])->whereNull('deleted_at')->paginate($per_page);
        $this->mappingApiKeys($apikeys);
        return $this->response(compact('apikeys'));
    }

    public function mappingApiKeys($apikeys)
    {
        foreach ($apikeys as $item) {
            if (!empty($item->type_id)) {
                $period = TimePeriod::find($item->type_id);
                $item['start_time'] = $period->start_time;
                $item['end_time'] = $period->end_time;
                if ($period->end_time < Carbon::now()) {
                    $item['status'] = 'outdated';
                    // $update_data['status'] = 'outdated';
                }
                // ApiKey::where('id', $item->id)->update($update_data);
            }
        }
    }

    public function apiGateways(Request $request)
    {
        $gateways = ApiGateway::where(['apikey_id' => $request->apikey_id, 'group' => $request->group])
                                ->select('apikey_id', 'group', 'endpoint')
                                ->get();
        return $this->response(compact('gateways'));
    }

    public function logs(Request $request)
    {
        $apikey_id = $request->apikey_id;
        // $page = $request->page;
        $per_page = $request->per_page;
        $logs = ApiKeyRequestLog::where('app_id', $apikey_id)->latest()->paginate($per_page);
        return $this->response(compact('logs'));
    }

    public function delete(Request $request)
    {
        $apikey_id = $request->get('apikey_id');
        $apikey = ApiKey::find($apikey_id);
        $this->checkParamError($apikey, trans('checker.check_apikey_id_error'));
        // $apikey->delete();
        return $this->response($apikey->delete());
    }
}
